document.addEventListener("DOMContentLoaded", function() {
    let reportForm = document.querySelector("form[action='']");
    let submitButton = document.getElementById("contact-submit");

    reportForm.addEventListener("submit", function(event) {
        event.preventDefault(); // 阻止表单默认提交行为

        let email = document.getElementById("email").value;
        let username = document.getElementById("username").value;
        let reportType = document.getElementById("reportType").value;
        let reportReason = document.getElementById("reportReason").value;
        let hiddenId = document.getElementById("hiddenId").value; // 获取隐藏的Id值

        if (email.trim() !== "" && username.trim() !== "" && reportType.trim() !== "" && reportReason.trim() !== "") {
            // 更新按钮文本为"正在提交..."
            submitButton.innerText = submitButton.getAttribute("data-submit-text");

            // 创建XMLHttpRequest对象
            let xhr = new XMLHttpRequest();

            // 设置请求方法和URL
            xhr.open("POST", "/submitReportMsg", true);

            // 设置请求头
            xhr.setRequestHeader("Content-Type", "application/json");

            // 处理响应
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    // 恢复按钮文本为原始文本
                    submitButton.innerText = submitButton.getAttribute("data-original-text");

                    if (xhr.status == 200) {
                        // 请求成功处理
                        window.location.href = "/feed";
                    } else {
                        // 请求失败处理，可以根据需要添加逻辑
                        alert("请求失败，请重试！");
                    }
                }
            };

            // 构建发送的数据
            let data = {
                email: email,
                username: username,
                reportType: reportType,
                reportReason: reportReason,
                id: hiddenId // 将Id添加到数据中
            };

            // 发送数据
            xhr.send(JSON.stringify(data));

        } else {
            // 输入框有空值时的提示逻辑
            alert("请输入完整的信息");
        }
    });
});
